//
//  HZPeiziMiddleView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/27.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziMiddleView.h"
#import "MYUpDownBtn.h"

@interface HZPeiziMiddleView()

@property (nonatomic, strong)MYUpDownBtn *pBuy;
@property (nonatomic, strong)MYUpDownBtn *pSell;
@property (nonatomic, strong)MYUpDownBtn *pCancel;

@end

@implementation HZPeiziMiddleView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon {
    _pBuy = [MYUpDownBtn new];
    _pSell = [MYUpDownBtn new];
    _pCancel = [MYUpDownBtn new];
    
//    [_pBuy setTitle:@"买入" forState:UIControlStateNormal];
    [_pBuy setImage:[UIImage imageNamed:@"trading_buy"] forState:UIControlStateNormal];
//    [_pSell setTitle:@"卖出" forState:UIControlStateNormal];
    [_pSell setImage:[UIImage imageNamed:@"trading_sell"] forState:UIControlStateNormal];
//    [_pCancel setTitle:@"撤单" forState:UIControlStateNormal];
    [_pCancel setImage:[UIImage imageNamed:@"trading_cancel"] forState:UIControlStateNormal];
    [_pBuy setTitleColor:kGRAYColor forState:UIControlStateNormal];
    [_pSell setTitleColor:kGRAYColor forState:UIControlStateNormal];
    [_pCancel setTitleColor:kGRAYColor forState:UIControlStateNormal];
    _pBuy.titleLabel.font = [UIFont systemFontOfSize:15];
    _pSell.titleLabel.font = [UIFont systemFontOfSize:15];
    _pCancel.titleLabel.font = [UIFont systemFontOfSize:15];
    [_pBuy addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [_pSell addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [_pCancel addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)initConstraints {
    [self addSubview:_pBuy];
    [self addSubview:_pSell];
    [self addSubview:_pCancel];
    
    [_pBuy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(_pSell.mas_left);
        make.bottom.mas_equalTo(self).offset(-5);
    }];
    [_pSell mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pBuy);
        make.right.mas_equalTo(_pCancel.mas_left);
    }];
    [_pCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pSell);
        make.right.mas_equalTo(self);
        make.width.mas_equalTo(@[_pBuy, _pSell]);
    }];
}

- (void)onClick:(UIButton *)btn
{
    if ([btn isEqual:_pCancel]) {
        LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterPeiziCancelOrder];
        if (block) {
            block(nil, nil);
        }
    }else {
        NSString *name = @"";
        if ([btn isEqual:_pBuy]) {
            name = @"买入";
        }else if ([btn isEqual:_pSell]) {
            name = @"卖出";
        }
        LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterPeiziBuySell];
        if (block) {
            block(nil, name);
        }
    }
}

@end
